﻿#include "guild_basic_logic.h"

#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>

#include "AnnounceMsg_struct.h"
#include "AnnounceData_struct.h"
#include "GuildModuleBrief_struct.h"
#include "GuildMember_struct.h"
#include "GuildModuleMembers_struct.h"

#include "GuildRedEnvelopeGrabber_struct.h"
#include "GuildRedEnvelopeBrief_struct.h"
#include "GuildRedEnvelope_struct.h"

#include "GuildModuleRedEnvelope_struct.h"

#include "GuildAuctionRole_struct.h"
#include "GuildAuctionRoleInfo_struct.h"
#include "GuildAuctionAttentionInfo_struct.h"
#include "GuildRepoCell_struct.h"
#include "GuildAuctionItem_struct.h"
#include "GuildModuleRepository_struct.h"

#include "GuildApplicationInfo_struct.h"
#include "GuildModuleApplication_struct.h"

#include "GuildBuilding_struct.h"
#include "GuildModuleBuilding_struct.h"

#include "error/make_error_code.h"
#include "guild/guild_dump.hpp"
#include "guild/module/guild_module.h"
#include "guild/module/guild_module_loader.hpp"
#include "guild_service_basic.h"

#include "guild/guild_loader.hpp"
#include "guild/guild_saver.hpp"

#include "util/time_util.hh"

namespace guild_service {

bool GuildBasicLogic::CreateGuild(const role_uuid_type role_uuid,
                                  const std::string &guild_name,
                                  guild_id_type &guild_id, error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id = GuildServiceBasicSingleton->GetGuildIdByRole(role_uuid);

  if (guild_id != kInvalidGuildId) {
    ec = ErrorCode::ERR_GUILD_ALREADY_EXIST;
    // LOG_WARN("公会已存在 角色唯一id:%llu 公会唯一id:%llu", role_uuid,
    // guild_id);
    return false;
  }

  if (!detail::IncrGuildGeneralId(kGuildServiceBasicName,
                                    detail::kLoadGuildLastIdKey, guild_id)) {
    ec = ErrorCode::ERR_GUILD_ADD_FAILED;
  }

  if (!detail::SaveGuildIdByRole(kGuildServiceBasicName, kRedisSetOp, role_uuid,
                                   guild_id)) {
    ec = ErrorCode::ERR_GUILD_MEMBERS_ADD_ID_FAILED;
    // LOG_WARN("保存角色查询公会id失败 角色唯一id:%llu 公会:%s", role_uuid,
    // DumpGuild(*guild_ptr).c_str());
    return false;
  }
  {
    GuildModuleBrief module_brief;
    
    GuildMember member;
    member.role_uuid = role_uuid;
    member.duty = duty_type::GUILD_DUTY_CHAIRMAN;
    GuildModuleMembers module_members{{member}};

    GuildModuleRedEnvelope module_red_envelope;

    GuildModuleRepository module_repository;

    GuildModuleApplication module_application;

    GuildModuleBuilding module_building;
    auto module_data_list = std::tie(
        module_brief, module_members, module_red_envelope, module_repository,
        module_application, module_building);

    std::vector<std::string> module_name_list{
        kGuildModuleBriefName,       kGuildModuleMembersName,
        kGuildModuleRedEnvelopeName, kGuildModuleRepositoryName,
        kGuildModuleApplicationName, kGuildModuleBuildingName
    };

    if (!detail::SaveGuildModuleList(kGuildServiceBasicName, module_data_list,
                                     guild_id, module_name_list)) {
      ec = ErrorCode::ERR_GUILD_MOD_REPOSITORY_SAVE_FAILED;
      return false;
    }
    // tuple_for_each(module_data_list, [](const auto &v) {
    //   LOG_DEBUG("Test tuple_at");
    // });
    // const auto &v = std::get<0>(tup);
    // std::tuple_for_each(p.reflect(), [](const auto& m) { 
    // }); 
  }
/** 逐个落地
  {
    GuildModuleBrief module_brief;
    module_brief.level = 1;
    module_brief.name = guild_name;
    module_brief.creator_uuid = role_uuid;
    module_brief.chairman_uuid = role_uuid;
    module_brief.is_auto_approval = false;

    if (!detail::SaveGuildModule<GuildModuleBrief>(
            kGuildServiceBasicName, module_brief, guild_id, kGuildModuleBriefName)) {
        ec = ErrorCode::ERR_GUILD_BRIEF_SAVE_FAILED;
        // LOG_FAIL("保存公会简略信息模块失败 角色唯一id:%llu 公会:%s",
        // role_uuid, DumpGuild(guild).c_str());
        return false;
    }
  }
  {
    GuildMember member;
    member.role_uuid = role_uuid;
    member.duty = duty_type::GUILD_DUTY_CHAIRMAN;

    GuildModuleMembers module_members{{member}};
    
    if (!detail::SaveGuildModule<GuildModuleMembers>(
            kGuildServiceBasicName, module_members, guild_id, kGuildModuleMembersName)) {
        ec = ErrorCode::ERR_GUILD_MEMBERS_SAVE_FAILED;
        // LOG_FAIL("保存公会成员模块失败 角色唯一id:%llu 公会:%s", role_uuid,
        // role_uuid, DumpGuild(guild).c_str());
        return false;
    }
  }
  {
    GuildModuleRedEnvelope module_red_envelope;
    module_red_envelope.red_envelope_id = 1;
    if (!detail::SaveGuildModule<GuildModuleRedEnvelope>(
            kGuildServiceBasicName, module_red_envelope, guild_id, kGuildModuleRedEnvelopeName)) {
      ec = ErrorCode::ERR_GUILD_MOD_RED_ENVELOPE_SAVE_FAILED;
      return false;
    }
  }
  {
    GuildModuleRepository module_repository;
    module_repository.last_modify_timestamp = kratos::util::get_os_time_millionsecond();
    if (!detail::SaveGuildModule<GuildModuleRepository>(
            kGuildServiceBasicName, module_repository, guild_id,
            kGuildModuleRepositoryName)) {
      ec = ErrorCode::ERR_GUILD_MOD_REPOSITORY_SAVE_FAILED;
      return false;
    }
  }
  {
    GuildModuleApplication module_application;
    module_application.last_modify_timestamp = kratos::util::get_os_time_millionsecond();
    if (!detail::SaveGuildModule<GuildModuleApplication>(
            kGuildServiceBasicName, module_application, guild_id,
            kGuildModuleApplicationName)) {
      ec = ErrorCode::ERR_GUILD_APPLICATION_SAVE_FAILED;
      return false;
    }
  }
  {
    GuildModuleBuilding module_building;
    module_building.last_modify_timestamp = kratos::util::get_os_time_millionsecond();
    module_building.building_dic.emplace(0, GuildBuilding{});
    module_building.building_dic.emplace(1, GuildBuilding{});
    module_building.building_dic.emplace(2, GuildBuilding{});
    if (!detail::SaveGuildModule<GuildModuleBuilding>(
            kGuildServiceBasicName, module_building, guild_id,
            kGuildModuleBuildingName)) {
      ec = ErrorCode::ERR_GUILD_MOD_BUILDING_SAVE_FAILED;
      return false;
    }
  }
*/
  return true;
}

bool GuildBasicLogic::DeleteGuild(const guild_id_type guild_id,
                                  error_code &ec) {
  if (guild_id == kInvalidGuildId) {
    ec = ErrorCode::ERR_INVALID_GUILD_ID;
    return false;
  }

  if (!GuildServiceBasicSingleton->RemoveGuild(guild_id)) {
    ec = ErrorCode::ERR_GUILD_DEL_FAILED;
    // LOG_WARN("移除公会失败 %s", DumpGuild(*guild_ptr).c_str());
    return false;
  }

  if (!GuildServiceBasic::DeleteGuildToRedis(guild_id)) {
    // LOG_WARN("删除公会失败 %s", DumpGuild(*guild_ptr).c_str());
    return false;
  }

  return true;
}

bool GuildBasicLogic::AddGuildRole(const guild_id_type guild_id,
                                   const role_uuid_type role_uuid) {
  return GuildServiceBasicSingleton->AddGuildRole(guild_id, role_uuid);
}

}  // namespace guild_service